package com.hhss.qishi.config;

import freemarker.ext.jsp.TaglibFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;

/**
 * web配置类
 * @author hhss
 * @date 2018年7月15日
 */
@Configuration
public class WebConfig implements WebMvcConfigurer{

	@Value("${QSApplication.annex.uploadURLPath}")
	private String uploadURLPath;

	@Value("${QSApplication.annex.uploadVirtualPath}")
	private String uploadVirtualPath;

	@Autowired(required = false)
	private FreeMarkerConfigurer configurer;

	private String annexVirtualPath;

	/**
	 * 定义静态变量
	 */
	@Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
		registry.addResourceHandler("/pages/**")
				.addResourceLocations("classpath:/pages/");
		registry.addResourceHandler("/surfaces/**")
				.addResourceLocations("classpath:/surfaces/");
		registry.addResourceHandler("/**")
				.addResourceLocations("classpath:/");
		// 为储存在服务器外部的文件，添加统一虚拟映射路径
		registry.addResourceHandler("/" + uploadURLPath + "**")
				.addResourceLocations("file:" + uploadVirtualPath);
    }
	
	/**
	 * exception异常配置
	 * @author Administrator
	 *
	 */
	@Component
	@PropertySource(value = {"exception_message.properties"}, encoding = "UTF-8")
	public class LoadProperty {

	}

	/**
	 * security标签
	 * 整合freemarker配置
	 */
	@PostConstruct
	public void freeMarkerConfigurer() {
		List<String> tal = new ArrayList<>();
		tal.add("/statics/tags/security.tld");
		TaglibFactory taglibFactory = configurer.getTaglibFactory();
		taglibFactory.setClasspathTlds(tal);
		if(taglibFactory.getObjectWrapper() == null) {
			taglibFactory.setObjectWrapper(configurer.getConfiguration().getObjectWrapper());
		}
	}
}